package com.jy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jy.common.enums.ResponseWrapper;
import com.jy.common.model.user.param.UserFollowerParam;
import com.jy.entity.UserFollow;
import com.jy.mapper.UserFollowMapper;
import com.jy.service.UserFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fzg
 * @since 2024-11-13
 */
@Service
public class UserFollowServiceImpl extends ServiceImpl<UserFollowMapper, UserFollow> implements UserFollowService {

    private final UserFollowMapper userFollowMapper;

    public UserFollowServiceImpl(UserFollowMapper userFollowMapper) {
        this.userFollowMapper = userFollowMapper;
    }

    @Override
    public ResponseWrapper followerAuthor(Integer currentUserAid, UserFollowerParam param) {

        UserFollow userFollow = userFollowMapper.selectOne(new LambdaQueryWrapper<UserFollow>()
                .eq(UserFollow::getFollowerAid, currentUserAid)
                .eq(UserFollow::getFollowerType, "用户")
                .eq(UserFollow::getFolloweredAid,param.getAuthorAid())
        );
        if (userFollow == null) {
            // 说明没有关注该动态的作者（去关注）
            UserFollow userFollow1 = new UserFollow();
            userFollow1.setFollowerAid(currentUserAid);
            userFollow1.setFolloweredAid(param.getAuthorAid());
            userFollow1.setFollowerType("用户");
            userFollowMapper.insert(userFollow1);
            return ResponseWrapper.markCustomSuccess("关注成功");
        }else {
            // 查询到了说明已经关注了(那就取消关注)
            userFollowMapper.deleteById(userFollow);
            return ResponseWrapper.markCustomSuccess("已取消关注");
        }
    }

    @Override
    public ResponseWrapper followerUser(Integer userAidByToken, Integer userAid) {

        UserFollow userFollow = userFollowMapper.selectOne(new LambdaQueryWrapper<UserFollow>()
                .eq(UserFollow::getFollowerAid,userAidByToken)
                .eq(UserFollow::getFolloweredAid,userAid)
                .eq(UserFollow::getFollowerType,"用户")
        );
        if (userFollow != null){
            return ResponseWrapper.markSuccess("您已经关注他了");
        }else {
            // 查询所有用户关注表
            // 1. 查询出 userfollow 表中已有的 aid 列表
            QueryWrapper<UserFollow> queryWrapper = new QueryWrapper<>();
            queryWrapper.select("aid");
            List<Integer> userfollowAidList = userFollowMapper.selectList(queryWrapper).stream()
                    .map(UserFollow::getAid)
                    .sorted(Comparator.naturalOrder())
                    .collect(Collectors.toList());
            // 2. 找出最小的不存在的 userAid
            int minNonExistingUserAid = 1;
            for (Integer userfollowAid : userfollowAidList) {
                if (userfollowAid >= minNonExistingUserAid) {
                    if (userfollowAid > minNonExistingUserAid) {
                        break;
                    } else {
                        minNonExistingUserAid++;
                    }
                }
            }
            int finalMinNonExistingUserAid = minNonExistingUserAid;
            int insert = userFollowMapper.insert(new UserFollow(){{
                setAid(finalMinNonExistingUserAid);
                setFollowerAid(userAidByToken);
                setFolloweredAid(userAid);
                setFollowerType("用户");
            }});
            if (insert > 0){
                return ResponseWrapper.markSuccess();
            }else {
                return ResponseWrapper.markError();
            }
        }
    }
}
